--[[
    author:ZYB
    time:2021-10-28 11:29:08
]]
local FOO = {_VERSION = "0.0.1"}
local os = os
local math = math
local string = string
local string_match = string.match
local string_format = string.format
local tonumber = tonumber
local math_floor = math.floor
local ipairs = ipairs
local math_max = math.max
local os_date = os.date
local os_time = os.time


--! @brief 获取日0点
--! @param time时间戳
--! @return 日零点时间戳
FOO.get_today_zero_time = function(time)
    time = time or os.time()
    local now_date = os.date("*t", time)
    local today_time = os.time{year=now_date.year, month=now_date.month, day=now_date.day, hour=0,min=0,sec=0}
    return today_time
end


--! @brief 获取周0点
--! @param time时间戳
--! @return 周零点时间戳
FOO.get_week_zero_time = function(time)
    time = time or os.time()
    local today = tonumber(os.date("%w", time))  --周几
    today = (today == 0) and 7 or today          -- today = 0 为周日， 周日按第7天算
    local today_zero = FOO.get_today_zero_time(time) --今日0点
    local weekly_zero_time = today_zero - ((today-1)*86400)
    return math.floor(weekly_zero_time)
end


--! @brief 获取月0点
--! @param time时间戳
--! @return 月零点时间戳
FOO.get_month_zero_time = function(time)
    time = time or os.time()
    local now_date = os.date("*t", time)
    local Today_Time = os.time{year=now_date.year, month=now_date.month, day=1, hour=0,min=0,sec=0}
    return Today_Time
end


--! @brief 比较两个时间是否是同一天
--! @param time1，time2 时间戳
--! @return true or false
FOO.is_same_day = function(time1, time2)
    local day1 = FOO.get_today_zero_time(time1)
    local day2 = FOO.get_today_zero_time(time2)
    return day1 == day2
end


--! @brief 比较两个时间是否是同一周
--! @param time1，time2 时间戳
--! @return true or false
FOO.is_same_week = function(time1, time2)
    local week1 = FOO.get_week_zero_time(time1)
    local week2 = FOO.get_week_zero_time(time2)
    return week1 == week2
end


--! @brief 比较两个时间是否是同一月
--! @param time1，time2 时间戳
--! @return true or false
FOO.is_same_month = function(time1, time2)
    local month1 = FOO.get_month_zero_time(time1)
    local month2 = FOO.get_month_zero_time(time2)
    return month1 == month2
end


--! @brief 比较两个时间相差多少天
--! @param time1，time2 时间戳
--! @return true or false
FOO.time_diff_by_day = function(time1, time2)
    local day1 = FOO.get_today_zero_time(time1)
    local day2 = FOO.get_today_zero_time(time2)
    return math.floor(math.abs(day1-day2) / 86400)
end


--! @brief 比较两个时间相差多少周
--! @param time1，time2 时间戳
--! @return true or false
FOO.time_diff_by_week = function(time1, time2)
    local day1 = FOO.get_week_zero_time(time1)
    local day2 = FOO.get_week_zero_time(time2)
    return math.floor(math.abs(day1-day2) / 86400 / 7)
end


--! @brief 比较两个时间相差多少月
--! @param time1，time2 时间戳
--! @return true or false
FOO.time_diff_by_month = function(time1, time2)
    local date1 = os.date("*t", time1 or os.time())
    local date2 = os.date("*t", time2 or os.time())
    local diff_month = math.abs((date1.year*12+date1.month) - (date2.year*12+date2.month))
    return math.floor(diff_month)
end


--------------------------------------------------------------
-- 说明：把当地时间转化为UNIX时间
-- 参数：s_time		格式为"1970-01-01"或者"1970-01-01 00:00:00"
-- 返回：n
FOO.date_to_unix = function(s_time)
	local n_year, n_month, n_day, n_hour, n_min, n_sec = string_match(s_time, '(%d+)%p(%d+)%p(%d+)%p*%s*(%d*)%p*(%d*)%p*(%d*)')
	n_year = tonumber(n_year) or 0
	n_month = tonumber(n_month) or 0
	n_day = tonumber(n_day) or 0
	n_hour = tonumber(n_hour) or 0
	n_min = tonumber(n_min) or 0
	n_sec = tonumber(n_sec) or 0
	local date = {year = n_year, month = n_month, day = n_day, hour = n_hour, min = n_min, sec = n_sec}
	return os_time(date)
end

--------------------------------------------------------------
-- 说明：把UNIX时间转化为当地格式时间
-- 参数：n_time
-- 返回：s 			格式为""1970-01-01 00:00:00"
FOO.unix_to_date = function(n_time)
	return  os_date("%Y-%m-%d %H:%M:%S", n_time)
end

return FOO